#include "baseInfoWidget.h"

BaseInfoWidget::BaseInfoWidget(QWidget *parent)
    : QWidget{parent}
{

    infoProcess = new QProcess(this);
    mainLayout = new QHBoxLayout(this);
    mainLayout->setContentsMargins(0,0,0,0);

    // 右侧屏幕区域

    rightLayout = new QGridLayout;
    rightLayout->setContentsMargins(5,5,5,5);
    rightLayout->setHorizontalSpacing(10);
    rightLayout->setVerticalSpacing(30);
    rightLayout->setAlignment(Qt::AlignTop);
    addInfoWidget();    // 添加组件


    // 左侧屏幕区域
    leftLayout = new QVBoxLayout;

    screenLabel = new QLabel;

//    screenLabel->setMaximumSize(300,400);

    getScreenShot();

    // 底部按钮
    buttonLayout = new QHBoxLayout;
    refreshBtn = new QPushButton("刷新");
    saveBtn = new QPushButton("保存");
    buttonLayout->addWidget(refreshBtn);
    buttonLayout->addWidget(saveBtn);

    leftLayout->addWidget(screenLabel);
    leftLayout->addLayout(buttonLayout);

    connect(refreshBtn, SIGNAL(clicked()), this, SLOT(getScreenShot()));
    connect(saveBtn, SIGNAL(clicked()), this, SLOT(saveImage()));

    mainLayout->addLayout(leftLayout);
    mainLayout->addLayout(rightLayout);
    mainLayout->setStretchFactor(leftLayout,1);
    mainLayout->setStretchFactor(rightLayout,1);


}

void BaseInfoWidget::addInfoWidget()
{

    QStringList labelNameString;
    QStringList commandString;

    labelNameString << "设备型号" << "屏幕尺寸" << "安卓版本" << "分辨率" << "序列号";

    QList<QStringList> commandStrings;
    commandStrings.append(QStringList() << "shell" << "getprop" << "ro.product.model");
    commandStrings.append(QStringList() << "shell" << "wm" << "size");
    commandStrings.append(QStringList() << "shell" << "getprop" << "ro.build.version.release");
    commandStrings.append(QStringList() << "shell" << "wm" << "density");
    commandStrings.append(QStringList() << "get-serialno");

    bool isFinish;
    QString infoString;
    QStringList infoStrings;

//    QString styleString = "background-color:red;border-width: 1px; height:20px; width:100px;";


    for(int i = 0; i < labelNameString.length(); i++){

        QLabel *labelNameLabel = new QLabel(labelNameString[i]);

        rightLayout->addWidget(labelNameLabel, i, 0);

        // 执行指令，获取信息
        infoProcess->start("adb", commandStrings[i]);
        isFinish = infoProcess->waitForFinished();

        if(isFinish){
            infoString = QString::fromLocal8Bit(infoProcess->readAllStandardOutput());
            infoString = infoString.split("\n")[0].trimmed();
            // 处理Physical size: 1080x2400有：的行
            infoStrings = infoString.split(":");
            if(infoStrings.length() > 1){
                infoString = infoStrings[1];
            }
            QLabel *infoLabel = new QLabel(infoString);

//            infoLabel->setAlignment(Qt::AlignCenter);
            rightLayout->addWidget(infoLabel, i,1);
        }else{
            infoString = QString::fromLocal8Bit(infoProcess->readAllStandardError());
            qDebug() << infoString;
        }
    }

}

void BaseInfoWidget::getScreenShot(){
//    infoProcess->start("adb", QStringList() << "exec-out" << "-p" << ">" << sSPath);
    infoProcess->setStandardOutputFile(this->SSPATH);   // 设置标准输出文件
    infoProcess->start("adb", QStringList() << "exec-out" << "screencap"<<"-p");
    if(infoProcess->waitForFinished()){
        // 修改伸缩性的问题
//        QPixmap p; // load pixmap
//        p.load(this->SSPATH);
//        int w = screenLabel->width();
//        int h = screenLabel->height();

//        screenLabel->setPixmap(p.scaled(w,h,Qt::KeepAspectRatio));
////        screenLabel->setAlignment(Qt::AlignTop);
//        screenLabel->setAlignment(Qt::AlignHCenter);

        QString stylesheet = "image:url(%1);image-position:center;";
        screenLabel->setStyleSheet(stylesheet.arg(this->SSPATH));

    }else{
        qDebug() <<"ScreenShot Faile";
    }
}

void BaseInfoWidget::saveImage(){
    QString saveFilePath = QFileDialog::getSaveFileName(
                this,tr("Save Image"),"/home",tr("Images (*.png *.bmp *.jpg)"));

    QImage *img = new QImage;
    img->load(this->SSPATH);
    if(saveFilePath != ""){
        // 处理打开窗口关闭导致的路径为空的问题
        if(img->save(saveFilePath, 0)){
            QMessageBox::information(0, "截图保存","保存成功");
        }else{
            QMessageBox::critical(0, "截图保存","保存失败");
        }
    }

}
